import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from unittest import TestCase
from ddt import ddt, data, unpack


@ddt
class TestLoan(TestCase):
    loanp_test_data = [
        ['lisi', '123456', '10000', '10'],
        ['lisi5', '123456', '10000', '10'],
        ['lisi', '123456', '100000000000000000', '10'],
        ['lisi', '123456', '10', '10'],
        ['lisi', '123456', '100', '366'],
        ['lisi', '123456', '100', '365']
    ]

    loana_test_data = [
        ['lisi', '123456', '10000', '10', 'admin', '123456']
    ]

    @data(*loanp_test_data)
    @unpack
    def testChange(self, username, password, loanamount, loanterm):
        # option = webdriver.ChromeOptions()
        # option.add_argument('--headless')
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 窗口最大化
        driver.maximize_window()

        driver.get("http://localhost:90")

        # time.sleep(2)

        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)

        # time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]').click()
        time.sleep(1)
        # 点击安全网贷
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]').click()
        time.sleep(1)
        # 输入借贷金额
        driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(loanamount)
        time.sleep(1)
        # 输入借贷期限
        driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(loanterm)
        time.sleep(1)
        # 点击申请按钮
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)
        # 点击个人中心
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]').click()
        time.sleep(1)
        # 点击我的借贷
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[2]').click()
        time.sleep(1)
        # 点击末页
        pages_bar = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul')
        pages = pages_bar.find_elements(By.TAG_NAME, 'li')
        page_num = len(pages)
        driver.find_element(By.XPATH, f'/html/body/div/div/main/div/div/div/div/div[2]/div[2]/div[3]/nav/ul/li[{page_num}]/a').click()
        time.sleep(1)
        # 获取末页记录数量
        element = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div[1]/table/tbody')
        child_elements = element.find_elements(By.TAG_NAME, 'tr')
        child_element_count = len(child_elements)
        # 定位到新申请贷款的记录
        new_record = driver.find_element(By.XPATH, f'/html/body/div/div/main/div/div/div/div/div[2]/div[1]/table/tbody/tr[{child_element_count}]')
        # 获取文本内容
        texts = []
        child_elements = new_record.find_elements(By.TAG_NAME, 'td')
        for i in child_elements:
            texts.append(i.text)
        print(texts)
        # 退出浏览器
        driver.quit()
        self.assertEquals((loanamount + '.00元'), texts[1])
        self.assertEquals((loanterm + '天'), texts[2])

    @data(*loana_test_data)
    @unpack
    def testChange(self, username, password, loanamount, loanterm, aname, apwd):
        # option = webdriver.ChromeOptions()
        # option.add_argument('--headless')
        # 创建谷歌浏览器对象
        driver = webdriver.Chrome()

        # 窗口最大化
        driver.maximize_window()

        driver.get("http://localhost:90")

        # time.sleep(2)

        # 输入用户名
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)

        # time.sleep(1)
        # 输入密码
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        # time.sleep(1)

        # 点击登陆
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)

        # 点击金融工具
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]').click()
        time.sleep(1)
        # 点击安全网贷
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[3]/ul/li[2]').click()
        time.sleep(1)
        # 输入借贷金额
        driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(loanamount)
        time.sleep(1)
        # 输入借贷期限
        driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(loanterm)
        time.sleep(1)
        # 点击申请按钮
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        time.sleep(1)
        # 点击确定
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)
        # 登录管理员账户
        driver.get("http://localhost:90")
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(aname)
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(apwd)
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击网贷管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]').click()
        time.sleep(1)
        # 点击网贷审核
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[5]/ul/li[1]').click()
        time.sleep(1)
        # 点击末页
        pages_bar = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul')
        pages = pages_bar.find_elements(By.TAG_NAME, 'li')
        page_num = len(pages)
        driver.find_element(By.XPATH, f'/html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul/li[{page_num}]/a').click()
        time.sleep(1)
        # 获取末页记录数量
        element = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody')
        child_elements = element.find_elements(By.TAG_NAME, 'tr')
        child_element_count = len(child_elements)
        # 定位到新申请贷款的记录并点击审核通过按钮
        driver.find_element(By.XPATH, f'/html/body/div/div/main/div/div/div/div/div[2]/div/table/tbody/tr[{child_element_count}]/td[7]/button[1]').click()
        time.sleep(1)
        # 点击确定按钮
        driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)
        # 登录用户账户
        driver.get("http://localhost:90")
        driver.find_element(By.XPATH, "//input[@id='username']").send_keys(username)
        driver.find_element(By.XPATH, "//input[@id='password']").send_keys(password)
        driver.find_element(By.XPATH, "//button[@id='login_btn']").click()
        time.sleep(1)
        # 点击个人中心
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]').click()
        time.sleep(1)
        # 点击银行卡管理
        driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[4]/ul/li[3]').click()
        time.sleep(1)
        # 获取银行卡余额
        money = driver.find_element(By.XPATH, '/html/body/div/div/main/div/div/div[2]/div/div/div[2]/h4[2]').text
        time.sleep(1)
        self.assertEqual('账户余额：20000.00元', money)
